package org.cl.persist.system.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.cl.persist.system.dao.mysql.mapper.MenuMapper;
import org.cl.persist.system.data.dto.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenglei
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> {

    /**
     * 获取显示菜单树结构
     * @param group
     * @return
     */
    public List<Tree<Integer>> getLeftTreeMenu(String group) {
        List<Menu> menus = super.list();
        List<TreeNode<Integer>> collect = menus.stream()
                .filter(menu -> Menu.Type.LEFT_MENU.getIndex() == menu.getType() && menu.getIsShow())
                .map(menu -> {
                    TreeNode<Integer> node = new TreeNode<>(menu.getId(), menu.getPId(), menu.getName(), menu.getSort());
                    node.setExtra(new HashMap<String, Object>(6){{
                        put("path", menu.getPath());
                        put("icon", menu.getIcon());
                        // 菜单名称
                        put("label", menu.getName());
                        put("keepAlive", menu.getKeepAlive());
                        put("permission", menu.getPermission());
                    }});
                    return node;
                }).collect(Collectors.toList());
        return TreeUtil.build(collect, Menu.Const.ROOT_PARENT);
    }
}
